package com.umiwe.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 操作消息提醒
 *
 * @author liximing
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Result<T>{
    /**
     * 1.code状态值：代表本次请求response的状态结果。
     */
    private Integer code;
    /**
     * 2.message描述：对本次状态码的描述。
     */
    private String message;
    /**
     * 3.data数据：本次返回的数据。
     */
    private T data;

    /**
     * 成功，创建ResResult：没data数据
     */
    public static Result suc() {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 成功，创建ResResult：有data数据
     */
    public static Result success(Object data) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    private void setData(T data) {
        this.data = data;
    }

    public T getData() {
        return this.data;
    }

    private void setMessage(String message) {
        this.message = message;
    }

    private void setCode(Integer code) {
        this.code = code;
    }

    /**
     * 失败，指定status、desc
     */
    public static Result fail(Integer code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败，指定ResultCode枚举
     */
    public static Result error(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    /**
     * 把ResultCode枚举转换为ResResult
     */
    private void setResultCode(ResultCode code) {
        this.code = code.code();
        this.message = code.message();
    }
}
